package com.example.clq.myapplication;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

    }

    private void initViews() {
        textView = findViewById(R.id.textView);

//        getDataByGET();
//        getDataByPOST();
          getPhoto();//异步下载网络图片
    }

    private void getPhoto() {
        String url = "http://crp.gdcp.cn/env/theme/gjy/image/lmy.png";
        Request request = new Request.Builder().url(url).build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
               InputStream inputStream = response.body().byteStream();
                String filepath="";
                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
                }else {
                    filepath = getFilesDir().getAbsolutePath();
                }
                File file = new File(filepath+"/gdcp.jpg");
                if(file!=null){

                    FileOutputStream fos = new FileOutputStream(file);
                    Integer i=0;
                    byte[] buffer = new byte[2048];
                    while ((i=inputStream.read(buffer))!=-1){
                        fos.write(buffer,0,i);
                    }
                    fos.flush();
                    fos.close();
                }
            }
        });

    }

    private void getDataByPOST() {
        RequestBody fromBody = new FormBody.Builder().add("ip","59.108.54.37").build();
        Request.Builder builder = new Request.Builder();
        Request request = builder.url("http://ip.taobao.com/service/getIpInfo.php")
        .method("POST",fromBody)
        .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call=  okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                textView.setText(response.body().string());
            }
        });
    }

    private void getDataByGET() {
        Request.Builder builder = new Request.Builder();
        builder.url("https://www.baidu.com/");
        builder.method("GET",null);
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = builder.build();
         Call call = okHttpClient.newCall(request);
         call.enqueue(new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {
             }

             @Override
             public void onResponse(Call call, Response response) throws IOException {
                textView.setText(response.body().string());
             }
         });
    }
}
